home *** CD-ROM | disk | FTP | other *** search
/ Practical Algorithms for Image Analysis / Practical Algorithms for Image Analysis.iso / TARFILE.GZ / tarfile / ch_2.3 / inv / inv.c next >
Encoding:
C/C++ Source or Header  |  1999-09-11  |  2.2 KB  |  122 lines

  1. /* 
  2.  * inv.c
  3.  * 
  4.  * Practical Algorithms for Image Analysis
  5.  * 
  6.  * Copyright (c) 1997, 1998, 1999 MLMSoftwareGroup, LLC
  7.  */
  8.  
  9. /*
  10.  * INV (Image inversion)
  11.  *
  12.  *  Inverts the input image and places the resulting
  13.  *  image in the output file
  14.  */
  15.  
  16. #include "inv.h"
  17.  
  18.  
  19. #define     ON        1
  20. #define     OFF        0
  21.  
  22. /*
  23.  * global variables
  24.  */
  25. extern char *optarg;
  26. extern int optind, opterr;
  27. extern short tiffInput;         /* flag=0 if no ImageIn to set tags; else =1 */
  28.  
  29.  
  30. /*
  31.  * usage of routine
  32.  */
  33. void
  34. usage (char *progname)
  35. {
  36.   progname = last_bs (progname);
  37.   printf ("USAGE: %s inimg outimg [-L]\n", progname);
  38.   printf ("\n%s inverts the input image and writes the resulting\n", progname);
  39.   printf ("image to the specified output file.\n\n");
  40.   printf ("ARGUMENTS:\n");
  41.   printf ("   inimg: input image filename (TIF)\n");
  42.   printf ("  outimg: ouput image filename (TIF)\n\n");
  43.   printf ("OPTIONS:\n");
  44.   printf ("      -L: print Software License for this module\n");
  45.   exit (1);
  46. }
  47.  
  48. void
  49. inv_image (Image * imgIn)
  50. {
  51.   int ix, iy;
  52.   int max_x, max_y;
  53.  
  54.   max_y = ImageGetHeight (imgIn);
  55.   max_x = ImageGetWidth (imgIn);
  56.   for (ix = 0; ix < max_x; ix++)
  57.     for (iy = 0; iy < max_y; iy++) {
  58.       setpixel (ix, iy, imgIn, (int) (~getpixel (ix, iy, imgIn)));
  59.     }
  60. }
  61.  
  62. void
  63. main (int argc, char *argv[])
  64. {
  65.   Image *imgIn;
  66.  
  67.   int i_arg;
  68.  
  69. /*
  70.  * cmd line options
  71.  */
  72.   static char *optstring = "L";
  73.  
  74.  
  75. /*
  76.  * parse command line
  77.  */
  78.   optind = 3;                   /* set getopt to point to the 3rd arg */
  79.   opterr = ON;                  /* give error messages */
  80.  
  81.  
  82.   if (argc < 3)
  83.     usage (argv[0]);
  84.  
  85.   while ((i_arg = getopt (argc, argv, optstring)) != EOF) {
  86.     switch (i_arg) {
  87.     case 'L':
  88.       print_sos_lic ();
  89.       exit (0);
  90.     default:
  91.       printf ("\ngetopt: unknown condition encountered\n");
  92.       exit (1);
  93.       break;
  94.     }
  95.   }
  96.  
  97. /*
  98.  * Read input image
  99.  */
  100.   imgIn = ImageIn (argv[1]);
  101.  
  102.   if (imgIn->bps == 8 && imgIn->spp == 3) {
  103.     printf ("Got RGB image!!!\nInput image must be Grayscale or B&W!!\n");
  104.     exit (1);
  105.   }
  106.  
  107. /*
  108.  * perform image inversion 
  109.  */
  110.   inv_image (imgIn);
  111.  
  112. /* 
  113.  * reset tiffInput so that we write a grayscale file (i.e tags are not copied)
  114.  */
  115.   tiffInput = 0;
  116.  
  117. /*
  118.  * Write the output image
  119.  */
  120.   ImageOut (argv[2], imgIn);
  121. }
  122.